﻿using Colyseus;
using Colyseus.Schema;
using NetWorkToolkit;
using System;
using System.Collections.Generic;
using System.Net;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using UnityEngine;
using UnityEngine.UI;

public class SocketIOExample : MonoBehaviour
{
    public static SocketIOExample Instance;

    public Button ConnectBtn;
    public Button GetRoomBtn;
    public Button CreateRoomBtn;
    public Button JoinRoomBtn;
    public Button LeaveRoomBtn;
    public Button SendMsgBtn;
    public Button StartGameBtn;
    public Button EndGameBtn;
    public Button UploadScoresBtn;
    public Button UploadNetSyncModuleInfo;
    public Button RemoveNetSyncModuleInfo;
    public Button HoldBtn;
    public Button UnBundleBtn;
    public Button ForceBindBtn;
    public Dropdown FrameRateDropdown;
    public Text msgTips;
    public Text roomInfo;

    [Space(20)]
    public string IPAddress;

    void Start()
    {

        //AllNetSyncModulePoseResp netresp = new AllNetSyncModulePoseResp() ;
        //netresp.datas = new Dictionary<string, NetSyncModulePoseResp>();
        //NetSyncModulePoseResp re;
        //re.belongid = "111";
        //re.moduleid = "222";
        //re.pose = new float[7];
        //netresp.datas.Add("key", re);
        //Debug.LogError(JsonUtility.ToJson(new Serialization<string,NetSyncModulePoseResp>(netresp.datas)));

        Instance = this;
        MessageManager.Instance.InitializeMessage();

        #region btn event
        ConnectBtn.onClick.AddListener(() =>
        {
            MessageManager.Instance.SendConnectServerMsg(IPAddress, "2567");
        });


        GetRoomBtn.onClick.AddListener(() =>
        {
            MessageManager.Instance.SendRefreshRoomList();
        });


        CreateRoomBtn.onClick.AddListener(() =>
        {
            MessageManager.Instance.SendCreateRoomMsg();
        });


        // 需要画出房间列表才管用,传入的roomid是房间唯一标识
        JoinRoomBtn.onClick.AddListener(() =>
        {
            MessageManager.Instance.SendJoinRoomByIdMsg("");
        });


        LeaveRoomBtn.onClick.AddListener(() =>
        {
            MessageManager.Instance.SendLeaveRoomMsg();
        });


        SendMsgBtn.onClick.AddListener(() =>
        {
            // MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, System.DateTime.Now.ToString());
            //StartCoroutine(SyncRun());
            //StartCoroutine(SyncRun1());
            //StartCoroutine(SyncRun2());
            //StartCoroutine(SyncRun3());
            //StartCoroutine(SyncRun4());
            //StartCoroutine(SyncRun5());
            //StartCoroutine(SyncRun6());
            //StartCoroutine(SyncRun7());
            //StartCoroutine(SyncRun8());
            //StartCoroutine(SyncRun9());
            //StartCoroutine(SyncRun10());
        });


        StartGameBtn.onClick.AddListener(() =>
        {
            MessageManager.Instance.SendStartGameMsg();
        });


        EndGameBtn.onClick.AddListener(() =>
        {
            MessageManager.Instance.SendEndGameMsg();
        });


        UploadNetSyncModuleInfo.onClick.AddListener(() =>
        {
            var ms = GameObject.FindObjectsOfType<NetSyncModule>();

            if (ms == null)
            {
                Debug.Log("Err");
            }

            List<NetSyncModule> nsList = new List<NetSyncModule>();

            foreach (var item in ms) nsList.Add(item);

            MessageManager.Instance.SendUploadAPartNetSyncModule(nsList);

            MessageManager.Instance.SendStartS2CUpdateNetModuleInfoMsg();
        });

        RemoveNetSyncModuleInfo.onClick.AddListener(() => 
        {
            MessageManager.Instance.SendRemoveNSM("1001");
        });


        HoldBtn.onClick.AddListener(() =>
        {
            NetSyncModule m = PlayerMe.Instance.GetNetModuleById("1001");
            MessageManager.Instance.SendChangeNetSyncModuleStatusMsg(
                m.transform.position, m.transform.rotation, m.ModuleId, ColyseusClient.instance.SessionID);
            m.SetSync(true);
        });

        ForceBindBtn.onClick.AddListener(() =>
        {
            MessageManager.Instance.SendChangeOwnershipOfNetModuleMsg("1001", true);
        });

        UnBundleBtn.onClick.AddListener(() =>
        {
            var d = PlayerMe.Instance.GetNetModuleById("1001");
            if (d != null)
                MessageManager.Instance.SendChangeOwnershipOfNetModuleMsg("1001", false);
        });

        FrameRateDropdown.onValueChanged.AddListener((num) =>
        {
            Debug.Log(num);
            if (num.Equals(0))
            {
                framerate = 30;
            }
            else if (num.Equals(1))
            {
                framerate = 60;
            }
            else
            {
                framerate = 90;
            }
        });

        #endregion
    }

    private void OnApplicationQuit()
    {
        StopAllCoroutines();
    }

    float framerate = 30;
    // stess testing
    private System.Collections.IEnumerator SyncRun()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun0=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun1()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun1=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun2()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun2=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun3()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun3=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun4()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun4=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun5()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun5=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun6()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun6=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun7()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun7=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun8()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun8=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun9()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun9=" + Time.time);
        }
    }
    private System.Collections.IEnumerator SyncRun10()
    {
        while (true)
        {
            yield return new WaitForSeconds(1f / framerate);
            MessageManager.Instance.SendCommondMsg(CommandMsgId.Test, "SyncRun10=" + Time.time);
        }
    }
    #region View
    public void ShowBtn(string btnName)
    {
        Debug.LogError(btnName);
        HoldBtn.gameObject.SetActive(btnName == "hold");
        UnBundleBtn.gameObject.SetActive(btnName == "unhold");
    }
    public void ShowCommandLog(string log)
    {
        msgTips.text = log;
    }
    public void ShowRoomInfo(string log)
    {
        roomInfo.text = log;
    }

    #endregion

}

